package de.luh.sim.ftn.graph;

import java.awt.geom.Point2D;
import java.util.ArrayList;


/**
 * 
 * @author Dominik Molitor
 * @version 0.1
 * 
 */
public class Node extends Point2D {

	private double x;
	private double y;
	private ArrayList<Edge> edges;
	private int id;
	private int visted;

	public Node(double x, double y, int id) {
		this.x = x;
		this.y = y;
		this.id = id;
		edges = new ArrayList<Edge>();
	}

	@Override
	public boolean equals(Object n) {
		if (((Point2D) n).getX() == x && ((Point2D) n).getY() == y) {
			return true;
		}
		return false;
	}

	public boolean isConnected() {
		return !edges.isEmpty();
	}

	public ArrayList<Node> getNeighbours() {
		ArrayList<Node> nodes = new ArrayList<Node>();
		for (Edge e : edges) {
			if (e.getN1().equals(this)) {
				nodes.add(e.getN2());
			} else {
				nodes.add(e.getN1());
			}
		}

		return nodes;
	}

	public void setX(double x) {
		this.x = x;
	}

	public void setY(double y) {
		this.y = y;
	}

	public void addEdge(Edge e) {
		edges.add(e);
	}

	public boolean containsEdge(Edge e) {
		if (edges.contains(e)) {
			return true;
		}
		return false;
	}

	public void removeEdge(Edge e) {
		edges.remove(e);
	}

	public void clear() {
		edges.clear();
	}

	public ArrayList<Edge> getEdges() {
		return edges;
	}

	public void setEdges(ArrayList<Edge> edges) {
		this.edges = edges;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Override
	public double getX() {
		return x;
	}

	@Override
	public double getY() {
		return y;
	}

	@Override
	public void setLocation(double x, double y) {
		this.x = x;
		this.y = y;
	}

	public int isVisted() {
		return visted;
	}

	public void setVisted(int visted) {
		this.visted = visted;
	}
	
	public Node copy(){
		Node n = new Node(x,y,id);
		ArrayList<Edge> edges = new ArrayList<Edge>();
		for (Edge e : this.edges) {
			edges.add(e.copy());
		}
		n.setEdges(edges);
		return n;
	}
	
	
	
	@Override
	public String toString() {
		String toString = "";
		toString += "ID: " + id + " // Location x:" + x + "  y:" + y + " // Connected Nodes:";
		if (!edges.isEmpty()) {
			for (Edge e : edges) {
				if (e.getN1().equals(this)) {
					toString += e.getN2().getId();
				} else {
					toString += e.getN1().getId();
				}
			}
		} else {
			toString += "x";
		}

		return toString;

	}
}
